package org.alvin.i18n.ui;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.google.common.collect.Lists;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;


public class I18nUtils {

    // RechargeGiftDetail.vue
    // BlacklistList.vue

    // "Test.vue", "StatisticsView.vue"  不要

    public static void main(String[] args) throws IOException {
//        String[] dirs = {"D:\\tangzhichao\\new_workspace\\xiaolan\\clientcharge\\xiaolan_app_v3\\pages" };
        String [] dirs = {"D:\\tangzhichao\\workspace\\xiaolan\\electrombileweb\\xblue_v1\\src\\zv3newui"};
        List<String> excluds = Lists.newArrayList(

        );
        File outFile = new File("D:/i18n.js");
        String version = "v2";
        I18nV2Service i18nV2Service = new I18nV2Service();
        if (version.equals("v3")) {
            i18nV2Service = new I18nV3Service();
        }
        JSONObject result = new JSONObject();
        LinkedList<String> list = Lists.newLinkedList();
        for (String dir : dirs) {
            list.add(dir);
        }
        int count = 0;
        fileLabel:
        while (!list.isEmpty()) {
            String file = list.removeFirst();
            File f = new File(file);
            if (f.isDirectory()) {
                if (f.getAbsolutePath().contains("newview")) {
                    continue;
                }
                if (f.listFiles() != null) {
                    for (File s : f.listFiles()) {
                        list.add(s.getAbsolutePath());
                    }
                }
                continue;
            }
            if (!f.getName().toLowerCase().endsWith(".vue")) {
                continue;
            }
            // 过滤，临时先解决问题
            if (excluds.contains(f.getName())) {
                System.out.println("过滤文件-----" + f.getName());
                continue fileLabel;
            }

            System.out.println(count++);
            System.out.println("处理文件:" + f.getAbsolutePath());
            String fileName = f.getName();
            fileName = fileName.substring(0, fileName.lastIndexOf("."));
            fileName = f.getParentFile().getName().concat("_").concat(fileName);
            JSONObject obj = i18nV2Service.tranfer(f, fileName);
            if (result.get(fileName) != null) {
                JSONObject oldOjb = result.getJSONObject(fileName);
                obj.putAll(oldOjb);
            }
            if(!obj.isEmpty()) {
                result.put(fileName, obj);
            }
        }
        String formatStr = JSON.toJSONString(result, SerializerFeature.PrettyFormat, SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteDateUseDateFormat);
        Files.write(Paths.get(outFile.getAbsolutePath()), formatStr.getBytes());
//       JSONObject res = tranfer(new File("D:\\AssociatioList.vue"), "AssociatioList");
//        System.out.println(res);
    }


}
